function [nrec] = thist2unv(xmin,xmax)

%  Initially copied from analysis2unv on 4/29/04 by B. Ross
%  This function will create a universal file containing time history
%  records from the Project structure.  The function will prompt for
%  which channels to export to the universal file.  This function uses
%  the SAVEN routines to create the universal file.
%
%  **NOTE**: If the user wishes to append a file, they must answer YES
%  to the OVERWRITE prompt the system generates when they select the file.
%  This function will then generate another prompt asking whether to append
%  or overwrite.  This is an issue with using the UIPUTFILE function.
%
%  On 5/26/04 modified routine to include the pass of xmin and xmax from the
%  shell program.  This allows the export program to export only the time segment
%  that has been selected in the shell window.

%  Last modified on 5/26/04 by B. Ross
%
%  [num_records_written] = thist2unv

% **************************************************************************

global Project;

nrec = 0;

%  Select Channels to Export

[fchan, lchan, varargout] = channel_select;

%  Prompt for UNV filename

% 	filename = [Project.Test_Item.Run.Setup_File '.unv']
% 
[fname,pname] = uiputfile([Project.Test_Item.Run.Setup_File '.unv'],...
                    'Select Export Universal File');
if isequal([fname, pname], [0,0])
    return
end
%  If file exists, prompt for Append or Overwrite
filename = fullfile(pname, fname);
if exist(filename) == 2
    [status] = unvquery;
else
    status = 1;
end 

%  Create default header

head = univhead('default');

%  Replace default header fields with our specific data

%  Common to all channels in Project Time Histories
absspc = 1;
head = univhead('xmin',Project.Test_Item.Run.Time_Ref(1),head);
head = univhead('xdelta',1/Project.Test_Item.Run.Sample_Rate,head);
head = univhead('functype',1,head); % Time Response
head = univhead('xtype',17,head); % Time
head = univhead('yntype',12,head); % Acceleration

%  For each channel selected ...

for i=fchan:lchan
    respdir = 0;
    switch Project.Test_Item.Run.Channel(i).Direction
		case 'X'
            respdir = 1;
		case 'x'
            respdir = 1;
        case 'R'
            respdir = 1;
        case 'r'
            respdir = 1;
		case 'Y'
            respdir = 2;    
		case 'y'
            respdir = 2;
        case 'T'
            respdir = 2;
        case 't'
            respdir = 2;
		case 'Z'
            respdir = 3;
		case 'z'
            respdir = 3;
        case 'Th'
            respdir = 3;
        case 'th'
            respdir = 3;
    end
	if Project.Test_Item.Run.Channel(i).Orientation == '-'
        respdir = -1 * respdir;
	end
	temp = size(Project.Test_Item.Run.Channel(i).Trans_Loc,2);
	respnam = [Project.Test_Item.Run.Channel(i).Coordinate ...
               Project.Test_Item.Run.Channel(i).Direction ...
               Project.Test_Item.Run.Channel(i).Orientation];
	
    %  Start with header that's been updated for current export
    header = head;
    %  Updata header fields for this channel    
	header = univhead('respdir',respdir,header);
	header = univhead('respnode',str2num(Project.Test_Item.Run.Channel(i).Coordinate),header);
	header = univhead('respnam',respnam,header);
	header = univhead('rec5',Project.Test_Item.Run.Channel(i).Trans_Loc,header);

    %  Check file status flag     
    if i == fchan+1 & status == 1
        status = 2;
%         else
%             status = 2;
    end
    
    %  Actually write the information to a file
	
% 	uniwrite(Project.Test_Item.Run.Channel(i).Time_His, header, filename, status);
	uniwrite_bpr(double(Project.Test_Item.Run.Channel(i).Time_His(xmin:xmax)),...
                 double(Project.Test_Item.Run.Time_Ref(xmin:xmax)),...
                 header, absspc, filename, status);
    nrec = nrec + 1;
end
message = sprintf('%d records of data written',nrec);
disp(message)

